package com.project.utils.ueditor;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * <p>ClassName: UEditorConfig</p>
 * <p>Description: UEditor 配置信息 Bean</p>
 * @Author  suking
 * @Date: 2018年4月19日
 */
public class UEditorConfig {
	/**
	 * @Fields CONFIG_FILE_NAME : 配置文件的名称
	 */
	public static final String CONFIG_FILE_NAME = "ueditor-config.json";
	public static UEditorConfig config; 
	
	/**
	 * @Fields SCRAWL_FILE_NAME : 涂鸦上传filename定义
	 */
	private final static String SCRAWL_FILE_NAME = "scrawl";
	/**
	 * @Fields REMOTE_FILE_NAME : 远程图片抓取filename定义
	 */
	private final static String REMOTE_FILE_NAME = "remote";
	/**
	 * <p>Title: getInstance</p>
	 * <p>Description: 单例工厂方法</p>
	 * @return UEditorConfig
	 */
	public static UEditorConfig getInstance () {
		if(config == null) {
			try {
				loadConfig();
				return config;
			} catch ( Exception e ) {
				return null;
			}
		}else {
			return config;
		}
	}
	
	private static void loadConfig() throws JsonParseException, JsonMappingException, IOException {
		InputStream inputStream = UEditorConfig.class.getClassLoader().getResourceAsStream(CONFIG_FILE_NAME);
		ObjectMapper mapper = new ObjectMapper();
		config = mapper.readValue(inputStream, UEditorConfig.class);
	}
	
	public Map<String, Object> getConfig ( int type ) {
		Map<String, Object> conf = new HashMap<String, Object>();
		String savePath = null;
		switch ( type ) {
			case UEActionMap.UPLOAD_FILE:
				conf.put( "isBase64", "false" );
				conf.put( "maxSize", getFileMaxSize());
				conf.put( "allowFiles", getFileAllowFiles() );
				conf.put( "fieldName", getFileFieldName());
				savePath = getFilePathFormat();
				break;
			case UEActionMap.UPLOAD_IMAGE:
				conf.put("isBase64", "false" );
				conf.put("maxSize", getImageMaxSize());
				conf.put("allowFiles", getImageAllowFiles());
				conf.put("fieldName", getImageFieldName());
				savePath = getImagePathFormat();
				break;
			case UEActionMap.UPLOAD_VIDEO:
				conf.put( "maxSize", getVideoMaxSize());
				conf.put( "allowFiles", getVideoAllowFiles());
				conf.put( "fieldName", getVideoFieldName());
				savePath = getVideoPathFormat();
				break;
			case UEActionMap.UPLOAD_SCRAWL:
				conf.put( "filename", SCRAWL_FILE_NAME );
				conf.put( "maxSize", getScrawlMaxSize());
				conf.put( "fieldName", getScrawlFieldName());
				conf.put( "isBase64", "true" );
				savePath = getVideoPathFormat();
				break;
			case UEActionMap.CATCH_IMAGE:
				conf.put( "filename", REMOTE_FILE_NAME );
				conf.put( "filter", getCatcherLocalDomain());
				conf.put( "maxSize", getCatcherMaxSize());
				conf.put( "allowFiles", getCatcherAllowFiles());
				conf.put( "fieldName", getCatcherFieldName());
				savePath = getCatcherPathFormat();
				break;
			case UEActionMap.LIST_IMAGE:
				conf.put( "allowFiles", getImageManagerAllowFiles());
				conf.put( "dir", getImageManagerListPath());
				conf.put( "count", getImageManagerListSize());
				break;
				
			case UEActionMap.LIST_FILE:
				conf.put( "allowFiles", getFileManagerAllowFiles());
				conf.put( "dir", getFileManagerListPath());
				conf.put( "count", getFileManagerListSize());
				break;
		}
		conf.put( "savePath", savePath );
		return conf;
	}
	/**
	 * @Fields imageActionName : 执行上传图片的action名称
	 */
	private String imageActionName;
	/**
	 * @Fields imageFieldName : 提交的图片表单名称
	 */
	private String imageFieldName;
	/**
	 * @Fields imageMaxSize : 上传大小限制，单位B
	 */
	private Integer imageMaxSize;
	/**
	 * @Fields imageAllowFiles : 上传图片格式显示
	 */
	private String[] imageAllowFiles;
	/**
	 * @Fields imageCompressEnable : 是否压缩图片,默认是true
	 */
	private Boolean imageCompressEnable;
	/**
	 * @Fields imageCompressBorder : 图片压缩最长边限制
	 */
	private Integer imageCompressBorder;
	/**
	 * @Fields imageInsertAlign : 插入的图片浮动方式
	 */
	private String imageInsertAlign;
	/**
	 * @Fields imageUrlPrefix : 图片访问路径前缀
	 */
	private String imageUrlPrefix;
	/**
	 * @Fields imagePathFormat : 上传保存路径,可以自定义保存路径和文件名格式 
	 */
	private String imagePathFormat;
	/* 涂鸦图片上传配置项 */
	/**
	 * @Fields scrawlActionName : 执行上传涂鸦的action名称
	 */
	private String scrawlActionName;
	/**
	 * @Fields scrawlFieldName : 提交的图片表单名称
	 */
	private String scrawlFieldName;
	/**
	 * @Fields scrawlPathFormat : 上传保存路径,可以自定义保存路径和文件名格式
	 */
	private String scrawlPathFormat;
	/**
	 * @Fields scrawlMaxSize : 上传大小限制，单位B
	 */
	private Integer scrawlMaxSize;
	/**
	 * @Fields scrawlUrlPrefix : 图片访问路径前缀
	 */
	private String scrawlUrlPrefix;
	/**
	 * @Fields scrawlInsertAlign : 插入的图片浮动方式 
	 */
	private String scrawlInsertAlign;
	/* 截图工具上传 */
	/**
	 * @Fields snapscreenActionName : 执行上传截图的action名称
	 */
	private String snapscreenActionName;
	/**
	 * @Fields snapscreenPathFormat : 上传保存路径,可以自定义保存路径和文件名格式 
	 */
	private String snapscreenPathFormat;
	/**
	 * @Fields snapscreenUrlPrefix : 图片访问路径前缀 
	 */
	private String snapscreenUrlPrefix;
	/**
	 * @Fields snapscreenInsertAlign : 插入的图片浮动方式
	 */
	private String snapscreenInsertAlign;
	/* 抓取远程图片配置 */
	private String[] catcherLocalDomain;
	/**
	 * @Fields catcherActionName : 执行抓取远程图片的action名称
	 */
	private String catcherActionName;
	/**
	 * @Fields catcherFiJeldName : 提交的图片列表表单名称
	 */
	private String catcherFieldName;
	/**
	 * @Fields catcherPathFormat : 上传保存路径,可以自定义保存路径和文件名格式
	 */
	private String catcherPathFormat;
	/**
	 * @Fields catcherUrlPrefix : 图片访问路径前缀
	 */
	private String catcherUrlPrefix;
	/**
	 * @Fields catcherMaxSize : 上传大小限制，单位B
	 */
	private Integer catcherMaxSize;
	/**
	 * @Fields catcherAllowFiles : 抓取图片格式显示
	 */
	private String[] catcherAllowFiles;
	/* 上传视频配置 */
	/**
	 * @Fields videoActionName : 执行上传视频的action名称
	 */
	private String videoActionName;
	/**
	 * @Fields videoFieldName : 提交的视频表单名称 
	 */
	private String videoFieldName;
	/**
	 * @Fields videoPathFormat : 上传保存路径,可以自定义保存路径和文件名格式 
	 */
	private String videoPathFormat;
	/**
	 * @Fields videoUrlPrefix : 视频访问路径前缀
	 */
	private String videoUrlPrefix;
	/**
	 * @Fields videoMaxSize : 上传大小限制，单位B，默认100MB
	 */
	private Integer videoMaxSize;
	/**
	 * @Fields videoAllowFiles : 上传视频格式显示
	 */
	private String[] videoAllowFiles;
	/* 上传文件配置 */
	/**
	 * @Fields fileActionName : controller里,执行上传视频的action名称
	 */
	private String fileActionName;
	/**
	 * @Fields fileFieldName : 提交的文件表单名称
	 */
	private String fileFieldName;
	/**
	 * @Fields filePathFormat : 上传保存路径,可以自定义保存路径和文件名格式
	 */
	private String filePathFormat;
	/**
	 * @Fields fileUrlPrefix : 文件访问路径前缀
	 */
	private String fileUrlPrefix;
	/**
	 * @Fields fileMaxSize : 上传大小限制，单位B，默认50MB
	 */
	private Integer fileMaxSize;
	/**
	 * @Fields fileAllowFiles : 上传文件格式显示
	 */
	private String[] fileAllowFiles;
	/* 列出指定目录下的图片 */
	/**
	 * @Fields imageManagerActionName : 执行图片管理的action名称
	 */
	private String imageManagerActionName;
	/**
	 * @Fields imageManagerListPath : 指定要列出图片的目录
	 */
	private String imageManagerListPath;
	/**
	 * @Fields imageManagerListSize : 每次列出文件数量
	 */
	private Integer imageManagerListSize;
	/**
	 * @Fields imageManagerUrlPrefix : 图片访问路径前缀
	 */
	private String imageManagerUrlPrefix;
	/**
	 * @Fields imageManagerInsertAlign : 插入的图片浮动方式
	 */
	private String imageManagerInsertAlign;
	/**
	 * @Fields imageManagerAllowFiles : 列出的文件类型
	 */
	private String[] imageManagerAllowFiles;
	/* 列出指定目录下的文件 */
	/**
	 * @Fields fileManagerActionName : 执行文件管理的action名称
	 */
	private String fileManagerActionName;
	/**
	 * @Fields fileManagerListPath : 指定要列出文件的目录
	 */
	private String fileManagerListPath;
	/**
	 * @Fields fileManagerUrlPrefix : 文件访问路径前缀
	 */
	private String fileManagerUrlPrefix;
	/**
	 * @Fields fileManagerListSize : 每次列出文件数量
	 */
	private Integer fileManagerListSize;
	/**
	 * @Fields fileManagerAllowFiles : 列出的文件类型
	 */
	private String[] fileManagerAllowFiles;
	
	
	@Override
	public String toString() {
		return "UEditorConfig [imageActionName=" + imageActionName + ", imageFieldName=" + imageFieldName
				+ ", imageMaxSize=" + imageMaxSize + ", imageAllowFiles=" + Arrays.toString(imageAllowFiles)
				+ ", imageCompressEnable=" + imageCompressEnable + ", imageCompressBorder=" + imageCompressBorder
				+ ", imageInsertAlign=" + imageInsertAlign + ", imageUrlPrefix=" + imageUrlPrefix + ", imagePathFormat="
				+ imagePathFormat + ", scrawlActionName=" + scrawlActionName + ", scrawlFieldName=" + scrawlFieldName
				+ ", scrawlPathFormat=" + scrawlPathFormat + ", scrawlMaxSize=" + scrawlMaxSize + ", scrawlUrlPrefix="
				+ scrawlUrlPrefix + ", scrawlInsertAlign=" + scrawlInsertAlign + ", snapscreenActionName="
				+ snapscreenActionName + ", snapscreenPathFormat=" + snapscreenPathFormat + ", snapscreenUrlPrefix="
				+ snapscreenUrlPrefix + ", snapscreenInsertAlign=" + snapscreenInsertAlign + ", catcherLocalDomain="
				+ Arrays.toString(catcherLocalDomain) + ", catcherActionName=" + catcherActionName
				+ ", catcherFiJeldName=" + catcherFieldName + ", catcherPathFormat=" + catcherPathFormat
				+ ", catcherUrlPrefix=" + catcherUrlPrefix + ", catcherMaxSize=" + catcherMaxSize
				+ ", catcherAllowFiles=" + Arrays.toString(catcherAllowFiles) + ", videoActionName=" + videoActionName
				+ ", videoFieldName=" + videoFieldName + ", videoPathFormat=" + videoPathFormat + ", videoUrlPrefix="
				+ videoUrlPrefix + ", videoMaxSize=" + videoMaxSize + ", videoAllowFiles="
				+ Arrays.toString(videoAllowFiles) + ", fileActionName=" + fileActionName + ", fileFieldName="
				+ fileFieldName + ", filePathFormat=" + filePathFormat + ", fileUrlPrefix=" + fileUrlPrefix
				+ ", fileMaxSize=" + fileMaxSize + ", fileAllowFiles=" + Arrays.toString(fileAllowFiles)
				+ ", imageManagerActionName=" + imageManagerActionName + ", imageManagerListPath="
				+ imageManagerListPath + ", imageManagerListSize=" + imageManagerListSize + ", imageManagerUrlPrefix="
				+ imageManagerUrlPrefix + ", imageManagerInsertAlign=" + imageManagerInsertAlign
				+ ", imageManagerAllowFiles=" + Arrays.toString(imageManagerAllowFiles) + ", fileManagerActionName="
				+ fileManagerActionName + ", fileManagerListPath=" + fileManagerListPath + ", fileManagerUrlPrefix="
				+ fileManagerUrlPrefix + ", fileManagerListSize=" + fileManagerListSize + ", fileManagerAllowFiles="
				+ Arrays.toString(fileManagerAllowFiles) + "]";
	}

	public String getImageActionName() {
		return imageActionName;
	}

	public void setImageActionName(String imageActionName) {
		this.imageActionName = imageActionName;
	}

	public String getImageFieldName() {
		return imageFieldName;
	}

	public void setImageFieldName(String imageFieldName) {
		this.imageFieldName = imageFieldName;
	}

	public Integer getImageMaxSize() {
		return imageMaxSize;
	}

	public void setImageMaxSize(Integer imageMaxSize) {
		this.imageMaxSize = imageMaxSize;
	}

	public String[] getImageAllowFiles() {
		return imageAllowFiles;
	}

	public void setImageAllowFiles(String[] imageAllowFiles) {
		this.imageAllowFiles = imageAllowFiles;
	}

	public Boolean getImageCompressEnable() {
		return imageCompressEnable;
	}

	public void setImageCompressEnable(Boolean imageCompressEnable) {
		this.imageCompressEnable = imageCompressEnable;
	}

	public Integer getImageCompressBorder() {
		return imageCompressBorder;
	}

	public void setImageCompressBorder(Integer imageCompressBorder) {
		this.imageCompressBorder = imageCompressBorder;
	}

	public String getImageInsertAlign() {
		return imageInsertAlign;
	}

	public void setImageInsertAlign(String imageInsertAlign) {
		this.imageInsertAlign = imageInsertAlign;
	}

	public String getImageUrlPrefix() {
		return imageUrlPrefix;
	}

	public void setImageUrlPrefix(String imageUrlPrefix) {
		this.imageUrlPrefix = imageUrlPrefix;
	}

	public String getImagePathFormat() {
		return imagePathFormat;
	}

	public void setImagePathFormat(String imagePathFormat) {
		this.imagePathFormat = imagePathFormat;
	}

	public String getScrawlActionName() {
		return scrawlActionName;
	}

	public void setScrawlActionName(String scrawlActionName) {
		this.scrawlActionName = scrawlActionName;
	}

	public String getScrawlFieldName() {
		return scrawlFieldName;
	}

	public void setScrawlFieldName(String scrawlFieldName) {
		this.scrawlFieldName = scrawlFieldName;
	}

	public String getScrawlPathFormat() {
		return scrawlPathFormat;
	}

	public void setScrawlPathFormat(String scrawlPathFormat) {
		this.scrawlPathFormat = scrawlPathFormat;
	}

	public Integer getScrawlMaxSize() {
		return scrawlMaxSize;
	}

	public void setScrawlMaxSize(Integer scrawlMaxSize) {
		this.scrawlMaxSize = scrawlMaxSize;
	}

	public String getScrawlUrlPrefix() {
		return scrawlUrlPrefix;
	}

	public void setScrawlUrlPrefix(String scrawlUrlPrefix) {
		this.scrawlUrlPrefix = scrawlUrlPrefix;
	}

	public String getScrawlInsertAlign() {
		return scrawlInsertAlign;
	}

	public void setScrawlInsertAlign(String scrawlInsertAlign) {
		this.scrawlInsertAlign = scrawlInsertAlign;
	}

	public String getSnapscreenActionName() {
		return snapscreenActionName;
	}

	public void setSnapscreenActionName(String snapscreenActionName) {
		this.snapscreenActionName = snapscreenActionName;
	}

	public String getSnapscreenPathFormat() {
		return snapscreenPathFormat;
	}

	public void setSnapscreenPathFormat(String snapscreenPathFormat) {
		this.snapscreenPathFormat = snapscreenPathFormat;
	}

	public String getSnapscreenUrlPrefix() {
		return snapscreenUrlPrefix;
	}

	public void setSnapscreenUrlPrefix(String snapscreenUrlPrefix) {
		this.snapscreenUrlPrefix = snapscreenUrlPrefix;
	}

	public String getSnapscreenInsertAlign() {
		return snapscreenInsertAlign;
	}

	public void setSnapscreenInsertAlign(String snapscreenInsertAlign) {
		this.snapscreenInsertAlign = snapscreenInsertAlign;
	}

	public String[] getCatcherLocalDomain() {
		return catcherLocalDomain;
	}

	public void setCatcherLocalDomain(String[] catcherLocalDomain) {
		this.catcherLocalDomain = catcherLocalDomain;
	}

	public String getCatcherActionName() {
		return catcherActionName;
	}

	public void setCatcherActionName(String catcherActionName) {
		this.catcherActionName = catcherActionName;
	}

	public String getCatcherFieldName() {
		return catcherFieldName;
	}

	public void setCatcherFieldName(String catcherFieldName) {
		this.catcherFieldName = catcherFieldName;
	}

	public String getCatcherPathFormat() {
		return catcherPathFormat;
	}

	public void setCatcherPathFormat(String catcherPathFormat) {
		this.catcherPathFormat = catcherPathFormat;
	}

	public String getCatcherUrlPrefix() {
		return catcherUrlPrefix;
	}

	public void setCatcherUrlPrefix(String catcherUrlPrefix) {
		this.catcherUrlPrefix = catcherUrlPrefix;
	}

	public Integer getCatcherMaxSize() {
		return catcherMaxSize;
	}

	public void setCatcherMaxSize(Integer catcherMaxSize) {
		this.catcherMaxSize = catcherMaxSize;
	}

	public String[] getCatcherAllowFiles() {
		return catcherAllowFiles;
	}

	public void setCatcherAllowFiles(String[] catcherAllowFiles) {
		this.catcherAllowFiles = catcherAllowFiles;
	}

	public String getVideoActionName() {
		return videoActionName;
	}

	public void setVideoActionName(String videoActionName) {
		this.videoActionName = videoActionName;
	}

	public String getVideoFieldName() {
		return videoFieldName;
	}

	public void setVideoFieldName(String videoFieldName) {
		this.videoFieldName = videoFieldName;
	}

	public String getVideoPathFormat() {
		return videoPathFormat;
	}

	public void setVideoPathFormat(String videoPathFormat) {
		this.videoPathFormat = videoPathFormat;
	}

	public String getVideoUrlPrefix() {
		return videoUrlPrefix;
	}

	public void setVideoUrlPrefix(String videoUrlPrefix) {
		this.videoUrlPrefix = videoUrlPrefix;
	}

	public Integer getVideoMaxSize() {
		return videoMaxSize;
	}

	public void setVideoMaxSize(Integer videoMaxSize) {
		this.videoMaxSize = videoMaxSize;
	}

	public String[] getVideoAllowFiles() {
		return videoAllowFiles;
	}

	public void setVideoAllowFiles(String[] videoAllowFiles) {
		this.videoAllowFiles = videoAllowFiles;
	}

	public String getFileActionName() {
		return fileActionName;
	}

	public void setFileActionName(String fileActionName) {
		this.fileActionName = fileActionName;
	}

	public String getFileFieldName() {
		return fileFieldName;
	}

	public void setFileFieldName(String fileFieldName) {
		this.fileFieldName = fileFieldName;
	}

	public String getFilePathFormat() {
		return filePathFormat;
	}

	public void setFilePathFormat(String filePathFormat) {
		this.filePathFormat = filePathFormat;
	}

	public String getFileUrlPrefix() {
		return fileUrlPrefix;
	}

	public void setFileUrlPrefix(String fileUrlPrefix) {
		this.fileUrlPrefix = fileUrlPrefix;
	}

	public Integer getFileMaxSize() {
		return fileMaxSize;
	}

	public void setFileMaxSize(Integer fileMaxSize) {
		this.fileMaxSize = fileMaxSize;
	}

	public String[] getFileAllowFiles() {
		return fileAllowFiles;
	}

	public void setFileAllowFiles(String[] fileAllowFiles) {
		this.fileAllowFiles = fileAllowFiles;
	}

	public String getImageManagerActionName() {
		return imageManagerActionName;
	}

	public void setImageManagerActionName(String imageManagerActionName) {
		this.imageManagerActionName = imageManagerActionName;
	}

	public String getImageManagerListPath() {
		return imageManagerListPath;
	}

	public void setImageManagerListPath(String imageManagerListPath) {
		this.imageManagerListPath = imageManagerListPath;
	}

	public Integer getImageManagerListSize() {
		return imageManagerListSize;
	}

	public void setImageManagerListSize(Integer imageManagerListSize) {
		this.imageManagerListSize = imageManagerListSize;
	}

	public String getImageManagerUrlPrefix() {
		return imageManagerUrlPrefix;
	}

	public void setImageManagerUrlPrefix(String imageManagerUrlPrefix) {
		this.imageManagerUrlPrefix = imageManagerUrlPrefix;
	}

	public String getImageManagerInsertAlign() {
		return imageManagerInsertAlign;
	}

	public void setImageManagerInsertAlign(String imageManagerInsertAlign) {
		this.imageManagerInsertAlign = imageManagerInsertAlign;
	}

	public String[] getImageManagerAllowFiles() {
		return imageManagerAllowFiles;
	}

	public void setImageManagerAllowFiles(String[] imageManagerAllowFiles) {
		this.imageManagerAllowFiles = imageManagerAllowFiles;
	}

	public String getFileManagerActionName() {
		return fileManagerActionName;
	}

	public void setFileManagerActionName(String fileManagerActionName) {
		this.fileManagerActionName = fileManagerActionName;
	}

	public String getFileManagerListPath() {
		return fileManagerListPath;
	}

	public void setFileManagerListPath(String fileManagerListPath) {
		this.fileManagerListPath = fileManagerListPath;
	}

	public String getFileManagerUrlPrefix() {
		return fileManagerUrlPrefix;
	}

	public void setFileManagerUrlPrefix(String fileManagerUrlPrefix) {
		this.fileManagerUrlPrefix = fileManagerUrlPrefix;
	}

	public Integer getFileManagerListSize() {
		return fileManagerListSize;
	}

	public void setFileManagerListSize(Integer fileManagerListSize) {
		this.fileManagerListSize = fileManagerListSize;
	}

	public String[] getFileManagerAllowFiles() {
		return fileManagerAllowFiles;
	}

	public void setFileManagerAllowFiles(String[] fileManagerAllowFiles) {
		this.fileManagerAllowFiles = fileManagerAllowFiles;
	}
	
}
